/*
Fast Artificial Neural Network Library (fann)
Copyright (C) 2003-2012 Steffen Nissen (sn@leenissen.dk)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <stdio.h>

#include "fann.h"

int FANN_API test_callback(struct fann *ann, struct fann_train_data *train,
	unsigned int max_epochs, unsigned int epochs_between_reports,
	float desired_error, unsigned int epochs)
{
	printf("Epochs     %8d. MSE: %.5f. Desired-MSE: %.5f\n", epochs, fann_get_MSE(ann), desired_error);
	return 0;
}




int main(int argc, char *argv[])
{
	const char* extIn = ".prenet";
	const char* extOut = ".tenet";

	if (argc <8)
	{
		printf("0\nBadNumberOfParams");
		return 0;
	}
	
	char* baseNetwork = argv[1];
	char* trainingFile = argv[2];
	float learningRate = atof(argv[3]);
	float learningMomentum = atof(argv[4]);
	int maxEpoc = atoi(argv[5]);
	float desiredError = atof(argv[6]);
	float bitFail = atof(argv[7]);
	
	/*char* baseNetwork = "aaaa";
	char* trainingFile = "dejm.dat";
	float learningRate = 1;
	float learningMomentum = 0.5;
	int maxEpoc = 4000;
	float desiredError = 0.01;
	*/


	char* sourceOut;
	struct fann *ann;

	char* sourceIn = (char *)malloc(1 + strlen(extIn) + strlen(baseNetwork));
	strcpy(sourceIn, baseNetwork);
	strcat(sourceIn, extIn);
	if (argc == 9)
	{
		char *outName = argv[8];
		sourceOut = (char *)malloc(1 + strlen(extOut) + strlen(outName));
		strcpy(sourceOut, outName);
		strcat(sourceOut, extOut);
	}
	else
	{
		sourceOut = (char *)malloc(1 + strlen(extOut) + strlen(baseNetwork));
		strcpy(sourceOut, baseNetwork);
		strcat(sourceOut, extOut);
	}


	ann = fann_create_from_file(sourceIn);
	struct fann_train_data *data;
	fann_set_bit_fail_limit(ann, bitFail);
	data = fann_read_train_from_file(trainingFile);
	fann_set_learning_rate(ann, learningRate);
	fann_set_learning_momentum(ann, learningMomentum);
	fann_init_weights(ann, data);
	fann_train_on_data(ann, data, maxEpoc, 0, desiredError);


	fann_save(ann, sourceOut);
	printf("1\n%s", sourceOut);
	fann_destroy(ann);
	fann_destroy_train(data);
	return 0;
}
